rm(list = ls())

# Check if the packages that we need are installed
want = c("haven", "ggthemes", "scales","plm","stargazer","margins","interplot","sjPlot","gtools","extrafont",
         "sjmisc","tidyverse", "questionr","viridis","effects",  "survey", "srvyr", "hrbrthemes", "ggpubr")
have = want %in% rownames(installed.packages())
# Install the packages that we miss
if ( any(!have) ) { install.packages( want[!have] ) }
# Load the packages
junk <- lapply(want, library, character.only = T)
# Remove the objects we created
rm(have, want, junk)



# load dataset
dl<-read_dta("~/Replication material/have_your_cake_data_3waves.dta")



## Evaluations of Brexit and expectations (market access and bilateral erosion)
dl$brexit_ukmodel[1]
dl$EU_reaction<- as.numeric(dl$EU_reaction)
dl$brexit_ukmodel<- scales::rescale(as.numeric(dl$brexit_ukmodel), to=c(5,1))

g1a <-ggplot(na.omit(select(subset(dl, wave==1),
                      EU_reaction,brexit_ukmodel)), 
       aes(x = factor(EU_reaction,
                      labels = c("Terminated",
                                 "Severely restricted",
                                 "Somewhat restricted",
                                 "Unchanged")), 
           y = brexit_ukmodel, 
           color = EU_reaction)) +
  geom_boxplot(size=1.2, width=.5) +
  scale_y_continuous(breaks=c(1,3,5), labels = c("A cautionary\nexample","Neither","A role\nmodel")) +
  labs(#title = "Expected EU market access
    #expected EU accomodation", 
    #       subtitle = "Oct.-Nov. 2019",
    x = "Expected EU market access",
    y = "Evaluations of Brexit strategy") +
  theme_minimal() +
  theme(legend.position = "none") +
  coord_flip()

g1a

dl$brexit_impactUK <- scales::rescale(as.numeric(dl$brexit_impactUK), to=c(5,1))

g1b <-ggplot(na.omit(select(subset(dl, wave==1),
                            EU_reaction,brexit_impactUK)), 
             aes(x = factor(EU_reaction,
                            labels = c("Terminated",
                                       "Severely restricted",
                                       "Somewhat restricted",
                                       "Unchanged")), 
                 y = brexit_impactUK, 
                 color = EU_reaction)) +
  geom_boxplot(size=1.2, width=.5) +
  scale_y_continuous(breaks=c(1,3,5), labels = c("Very\nnegative","Neither","Very\npositive")) +
  labs(#title = "Expected EU market access
    #expected EU accomodation", 
    #       subtitle = "Oct.-Nov. 2019",
    x = "Expected EU market access",
    y = "Evaluations of Brexit impact") + 
  theme_minimal() + 
  theme(legend.position = "none") +
  coord_flip()

g1b


dl$bilateral_erosion[1]
dl$bilateral_erosion <- scales::rescale(as.numeric(dl$bilateral_erosion), to=c(5,1))
g2a<-ggplot(na.omit(select(subset(dl, wave==1),
                      bilateral_erosion,brexit_ukmodel)), 
       aes(x = factor(bilateral_erosion,
                      labels = c("Very negative",
                                 "Somewhat negative",
                                 "Neither",
                                 "Somewhat positive",
                                 "Very positive")), 
           y = brexit_ukmodel, 
           color = bilateral_erosion)) +
  geom_boxplot(size=1.2, width=.5) +
  scale_y_continuous(breaks=c(1,3,5), labels = c("A cautionary\nexample","Neither","A role\nmodel")) +
  labs(#title = "Expected EU market access
    #expected EU accomodation", 
    #       subtitle = "Oct.-Nov. 2019",
    x = "Assessments of erosion of bilateral treaties",
    y = "Evaluations of Brexit strategy") +
  theme_minimal() +
  theme(legend.position = "none") +
  coord_flip()


g2a

g2b<-ggplot(na.omit(select(subset(dl, wave==1),
                           bilateral_erosion,brexit_impactUK)), 
            aes(x = factor(bilateral_erosion,
                           labels = c("Very negative",
                                      "Somewhat negative",
                                      "Neither",
                                      "Somewhat positive",
                                      "Very positive")), 
                y = brexit_impactUK, 
                color = bilateral_erosion)) +
  geom_boxplot(size=1.2, width=.5) +
  scale_y_continuous(breaks=c(1,3,5), labels = c("Very\nnegative","Neither","Very\npositive")) +
  labs(#title = "Expected EU market access
    #expected EU accomodation", 
    #       subtitle = "Oct.-Nov. 2019",
    x = "Assessments of erosion of bilateral treaties",
    y = "Evaluations of Brexit impact") +
  theme_minimal() +
  theme(legend.position = "none") +
  coord_flip()


g2b
g1a

?ggarrange
g_exp <- ggarrange(g1a, g1b,
          g2a,g2b, 
          align= "hv", nrow = 2, ncol=2)





table(dl$limitation_vote)
dl$limitation_vote <- ifelse(dl$limitation_vote>4, NA, dl$limitation_vote)
dl$limitation_vote<- scales::rescale(as.numeric(dl$limitation_vote), to=c(4,1))


d1a <-ggplot(na.omit(select(subset(dl, wave==1),
                           limitation_vote,brexit_ukmodel)), 
            aes(x = factor(limitation_vote,
                           labels = c("Certainly against",
                                      "Rather against",
                                      "Rather in favour",
                                      "Certainly in favour")), 
                y = brexit_ukmodel, 
                color = limitation_vote)) +
  geom_boxplot(size=1.2, width=.5) +
  scale_y_continuous(breaks=c(1,3,5), labels = c("A cautionary\nexample","Neither","A role\nmodel")) +
  labs(#title = "Expected EU market access
    #expected EU accomodation", 
    #       subtitle = "Oct.-Nov. 2019",
    x = "Vote on the Limitation initiative",
    y = "Evaluations of Brexit strategy") +
  theme_minimal() +
  theme(legend.position = "none") +
  coord_flip()

d1a



d1b <-ggplot(na.omit(select(subset(dl, wave==1),
                           limitation_vote,brexit_impactUK)), 
            aes(x = factor(limitation_vote,
                           labels = c("Certainly against",
                                      "Rather against",
                                      "Rather in favour",
                                      "Certainly in favour")), 
                y = brexit_impactUK, 
                color = limitation_vote)) +
  geom_boxplot(size=1.2, width=.5) +
  scale_y_continuous(breaks=c(1,3,5), labels = c("Very\nnegative","Neither","Very\npositive")) +
  labs(#title = "Expected EU market access
    #expected EU accomodation", 
    #       subtitle = "Oct.-Nov. 2019",
    x = "Vote on the Limitation initiative",
    y = "Evaluations of Brexit impact") +
  theme_minimal() +
  theme(legend.position = "none") +
  coord_flip()

d1b

dl$insta_vote[1]
table(dl$insta_vote)
dl$insta_vote <- ifelse(dl$insta_vote>4, NA, dl$insta_vote)

d2a<-ggplot(na.omit(select(subset(dl, wave==1),
                          insta_vote,brexit_ukmodel)), 
           aes(x = factor(insta_vote,
                          labels = c("Certainly in favour",
                                     "Rather in favour",
                                     "Rather against",
                                     "Certainly against")), 
               y = brexit_ukmodel, 
               color = insta_vote)) +
  geom_boxplot(size=1.2, width=.5) +
  scale_y_continuous(breaks=c(1,3,5), labels = c("A cautionary\nexample","Neither","A role\nmodel")) +
  labs(#title = "Expected EU market access
    #expected EU accomodation", 
    #       subtitle = "Oct.-Nov. 2019",
    x = "Vote on the Framework Agreement",
    y = "Evaluations of Brexit strategy") +
  theme_minimal() +
  theme(legend.position = "none") +
  coord_flip()

d2a


d2b<-ggplot(na.omit(select(subset(dl, wave==1),
                          insta_vote,brexit_impactUK)), 
           aes(x = factor(insta_vote,
                          labels = c("Certainly in favour",
                                     "Rather in favour",
                                     "Rather against",
                                     "Certainly against")), 
               y = brexit_impactUK, 
               color = insta_vote)) +
  geom_boxplot(size=1.2, width=.5) +
  scale_y_continuous(breaks=c(1,3,5), labels = c("Very\nnegative","Neither","Very\npositive")) +
  labs(#title = "Expected EU market access
    #expected EU accomodation", 
    #       subtitle = "Oct.-Nov. 2019",
    x = "Vote on the Framework Agreement",
    y = "Evaluations of Brexit  impact") +
  theme_minimal() +
  theme(legend.position = "none") +
  coord_flip()

d2b

library(ggpubr)
?ggarrange
exp <- ggarrange(g1a, g1b,
                 g2a,g2b, 
                 align= "hv", nrow = 2, ncol=2)
exp
ggsave("/Users/malet/Dropbox/Working papers/Swiss expectations/Graphs/bivariate_exp.jpg",  width = 22, height = 15, units = "cm")


vot <- ggarrange(d1a, d1b,
                 d2a,d2b, 
                 align= "hv", nrow = 2, ncol=2)
vot
ggsave("/Users/malet/Dropbox/Working papers/Swiss expectations/Graphs/bivariate_vot.jpg",  width = 22, height =  15, units = "cm")





# Expectation 1 : market access


val_labels(dw$EU_reaction_W1)<- c("terminated"=1, 
                                 "severely\nrestricted"=2, 
                                 "somewhat\nrestricted"=3,
                                 "unchanged"=4)
dw$EU_reaction_W1<-as_factor(dw$EU_reaction_W1)


ggplot2.barplot(data=df1, xName='time', yName="total_bill",
                groupName='sex', groupColors=c('#999999','#E69F00'),
                position=position_dodge(),
                #background and line colors
                backgroundColor="white", color="black", 
                xtitle="Time of day", ytitle="Total bill", 
                mainTitle="Total bill\n per time of day",
                removePanelGrid=TRUE,removePanelBorder=TRUE,
                axisLine=c(0.5, "solid", "black")
) 



p1 <- ggplot(dw[!is.na(dw$EU_reaction_W1), ], aes(EU_reaction_W1, weight=weight1)) + 
  geom_bar(aes(y = (..count..)/sum(..count..)), color="red4", fill="red4") + 
  scale_y_continuous(labels=scales::percent,breaks=c(0,.1,.2,.3,.4),limits = c(0,.51)) +
  geom_hline(yintercept=c(.1,.2,.3,.4), color="white", size=.2)+
  labs(title="Expected EU reaction to a Swiss withdrawal\nfrom the treaty on free movement of people",
       x="\nMarket access after approval of limitation initiative", y = "Relative frequencies") + theme_bw()+
  theme(plot.margin = unit(c(1, 1, 2, 1), "lines"),
        text=element_text(family = "LM Roman 10", size=12),
        axis.text=element_text(size=12, color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        plot.title = element_text(hjust = 0.5, size = 13, face ="bold"),
        strip.background = element_rect(colour="black",
                                        fill="white")   )
  

p1


# Expectation 2 : bilateral erosions

table(dw$bilateral_erosion_W1)
dw$bilateral_erosion_W1 <- scales::rescale(as.numeric(dw$bilateral_erosion_W1), to=c(5,1))
val_labels(dw$bilateral_erosion_W1)<- c("very\nnegative"=1, 
                                  "somewhat\nnegative"=2, 
                                  "neither"=3,
                                  "somewhat\npositive"=4,
                                  "very\npositive"=5)
dw$bilateral_erosion_W1<-as_factor(dw$bilateral_erosion_W1)



p2 <- ggplot(dw[!is.na(dw$bilateral_erosion_W1), ], aes(bilateral_erosion_W1, weight=weight1)) + 
  geom_bar(aes(y = (..count..)/sum(..count..)), color="red4", fill="red4") + 
  scale_y_continuous(labels=scales::percent, breaks=c(0,.1,.2,.3,.4), limits = c(0,.51)) +
  geom_hline(yintercept=c(.1,.2,.3,.4), color="white", size=.2)+
  labs(title="Expected consequences of an erosion of bilateral\ntreaties after rejection of framework agreement",
       x="\nConsequences of an erosion of  bilateral treaties", y = "Relative frequencies") + theme_bw()+
  theme(plot.margin = unit(c(1, 1, 2, 1), "lines"),
        text=element_text(family = "LM Roman 10", size=12),
        axis.text=element_text(size=12, color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        plot.title = element_text(hjust = 0.5, size = 13, face ="bold"),
        strip.background = element_rect(colour="black",
                                        fill="white")   )


p2



expect <- ggarrange(p1,p2, nrow=1)
expect
ggsave("/Users/malet/Dropbox/Work in progress/Swiss expectations/Graphs/descript_expectations.jpg",  
       width = 25, height =  13, units = "cm")



# Brexit 1 : outcome

prop.table(table(dw$brexit_impactUK_W1))
dw$brexit_impactUK_W1 <- scales::rescale(as.numeric(dw$brexit_impactUK_W1), to=c(5,1))

val_labels(dw$brexit_impactUK_W1)<- c("much\nworse"=1, 
                                      "somewhat\nworse"=2, 
                                      "neither"=3,
                                      "somewhat\nbetter"=4,
                                      "much\nbetter"=5)
dw$brexit_impactUK_W1<-as_factor(dw$brexit_impactUK_W1)



p3 <- ggplot(dw[!is.na(dw$brexit_impactUK_W1), ], aes(brexit_impactUK_W1, weight=weight1)) + 
  geom_bar(aes(y = (..count..)/sum(..count..)), color="red4", fill="red4") + 
  scale_y_continuous(labels=scales::percent,breaks=c(0,.1,.2,.3,.4),limits = c(0,.35)) +
  geom_hline(yintercept=c(.1,.2,.3), color="white", size=.2)+
  labs(title="Assessment of Brexit outcome for the UK",
       x="\nAfter Brexit, the UK will be...", y = "Relative frequencies") + theme_bw()+
  theme(plot.margin = unit(c(1, 1, 2, 1), "lines"),
        text=element_text(family = "LM Roman 10", size=12),
        axis.text=element_text(size=10, color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        plot.title = element_text(hjust = 0.5, size = 13, face ="bold"),
        strip.background = element_rect(colour="black",
                                        fill="white")   )


p3


# Brexit 1 : outcome

prop.table(table(dw$brexit_ukmodel_W1))
dw$brexit_ukmodel_W1 <- scales::rescale(as.numeric(dw$brexit_ukmodel_W1), to=c(5,1))

val_labels(dw$brexit_ukmodel_W1)<- c("a clear\nrole model"=5, 
                                      "rather\na model"=4, 
                                      "neither"=3,
                                      "rather\ncautionary"=2,
                                      "a very cautionary\nexample"=1)
dw$brexit_ukmodel_W1<-as_factor(dw$brexit_ukmodel_W1)



p4 <- ggplot(dw[!is.na(dw$brexit_ukmodel_W1), ], aes(brexit_ukmodel_W1, weight=weight1)) + 
  geom_bar(aes(y = (..count..)/sum(..count..)), color="red4", fill="red4") + 
  scale_y_continuous(labels=scales::percent,breaks=c(0,.1,.2,.3,.4),limits = c(0,.35)) +
  geom_hline(yintercept=c(.1,.2,.3), color="white", size=.2)+
  labs(title="Assessment of British Brexit strategy",
       x="\nFor Switzerland, the British strategy is...", y = "Relative frequencies") + theme_bw()+
  theme(plot.margin = unit(c(1, 1, 2, 1), "lines"),
        text=element_text(family = "LM Roman 10", size=12),
        axis.text=element_text(size=10, color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        plot.title = element_text(hjust = 0.5, size = 13, face ="bold"),
        strip.background = element_rect(colour="black",
                                        fill="white")   )


p4




brexit <- ggarrange(p3,p4, nrow=1)
brexit
ggsave("/Users/malet/Dropbox/Work in progress/Swiss expectations/Graphs/descript_brexit.jpg",  
       width = 25, height =  13, units = "cm")




# Political knowledge

prop.table(table(dw$knowledge))
prop.table(table(dw$cooperation_self_W1))

val_labels(dw$knowledge)<- c("0\n"=0,
                               "1\n"=1,
                               "2\n"=2,
                               "3\n"=3,
                               "4\n"=4,
                               "5\n"=5,
                               "6\n"=6)
dw$knowledge<-as_factor(dw$knowledge)


p5 <- ggplot(dw[!is.na(dw$knowledge), ], aes(knowledge, weight=weight1)) + 
  geom_bar(aes(y = (..count..)/sum(..count..)), color="red4", fill="red4") + 
  scale_y_continuous(labels=scales::percent,breaks=c(0,.1,.2,.3),limits = c(0,.35)) +
  geom_hline(yintercept=c(.1,.2,.3), color="white", size=.2)+
  labs(title="Political knowledge",
       x="\nNo. of correct answers to factual questions", y = "Relative frequencies") + theme_bw()+
  theme(plot.margin = unit(c(1, 1, 2, 1), "lines"),
        text=element_text(family = "LM Roman 10", size=12),
        axis.text=element_text(size=12, color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        plot.title = element_text(hjust = 0.5, size = 13, face ="bold"),
        strip.background = element_rect(colour="black",
                                        fill="white")   )


p5


# Brexit 1 : outcome
dw$cooperation_self_W1[1]
?wtd.table
prop.table(table(factor(dw$cooperation_self_W1)))
prop.table(wtd.table(factor(dw$cooperation_self_W1), weights=dw$weight1))
dw$cooperation <- scales::rescale(as.numeric(dw$cooperation_self_W1), to=c(7,1))
val_labels(dw$cooperation)<- c("strengthen\ncooperation"=7,
                               "maintain\nstatus-quo"=4,
                               "increase\nautonomy"=1)
dw$cooperation<-as_factor(dw$cooperation)



p6 <- ggplot(dw[!is.na(dw$cooperation), ], aes(cooperation, weight=weight1)) + 
  geom_bar(aes(y = (..count..)/sum(..count..)), color="red4", fill="red4") + 
  scale_y_continuous(labels=scales::percent,breaks=c(0,.1,.2,.3,.4),limits = c(0,.36)) +
  scale_x_discrete(breaks=c("increase\nautonomy",
                            "maintain\nstatus-quo",
                            "strengthen\ncooperation"))+
  geom_hline(yintercept=c(.1,.2,.3), color="white", size=.2)+
  labs(title="Attitudes towards Swiss-EU relations",
       x="\nPreferred evolution of CH-EU relations", y = "Relative frequencies") + theme_bw()+
  theme(plot.margin = unit(c(1, 1, 2, 1), "lines"),
        text=element_text(family = "LM Roman 10", size=12),
        axis.text=element_text(size=12, color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        plot.title = element_text(hjust = 0.5, size = 13, face ="bold"),
        strip.background = element_rect(colour="black",
                                        fill="white")   )


p6




moder <- ggarrange(p5,p6, nrow=1)
moder
ggsave("/Users/malet/Dropbox/Work in progress/Swiss expectations/Graphs/descript_moderators.jpg",  
       width = 25, height =  13, units = "cm")









df$brexit_impactUK[1]
df$breximp <- ifelse(df$brexit_impactUK<4,"better/same","worse")
df$brexit_ukmodel[1]
table(df$brexit_ukmodel)
df$strategy <- ifelse(df$brexit_ukmodel<4,"role model/neither","cautionary example")
df$breximp  <- factor(df$breximp  , levels = c("worse", "better/same"))


limit_impact <- dplyr::select(df, 
                          scenario_limitation0, 
                         weight3,
                          breximp, cooperation3)

ds<- limit_impact %>% 
  filter(complete.cases(.))

mu  <- ds %>%
  group_by(breximp, cooperation3) %>%
  summarize(grp_means = weighted.mean(scenario_limitation0, weight3))

ds$cooperation3 = as.factor(ds$cooperation3)


x_lab <- c("     Less","0", "More     ")



# Density plots with semi-transparent fill
lim_impact<-ggplot(na.omit(ds), aes(x=scenario_limitation0, color=breximp, fill=breximp)) + 
  geom_density(aes(weight=weight3),alpha=.2)+
  geom_vline(aes(xintercept = 0),linetype="dotted")+
  geom_vline(data = mu, aes(xintercept = grp_means, 
                                       color = breximp), linetype="dashed",size=.7)+ 
  facet_wrap(~cooperation3) + #,labeller = labeller(Scenario=scenario_labs))+
  labs(title="Three years after approval of limitation initiative",
       x="Expected change in Swiss-EU cooperation", y = "Density") +
  theme_bw()+
  theme(plot.margin = unit(c(1, 1, 2, 1), "lines"),
        text=element_text(family = "LM Roman 10"),
        axis.text=element_text(size=9, color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        legend.position ="bottom",
        legend.text = element_text(size = 11),
        plot.title = element_text(hjust = 0.5, size = 12, face ="bold"),
        plot.caption = element_text(hjust = 1, vjust = -3),
        strip.background = element_rect(colour="black",
                                        fill="white"),
        strip.text = element_text(
          size = 11, #face = "bold"
        ))+
  scale_x_continuous(breaks=c(-8,0,8),labels=x_lab )+ 
  scale_color_discrete(name = "After Brexit, the UK will be", 
                      labels=c( "worse", "better/same"))+ guides(fill="none")
  

lim_impact



insta_impact <- dplyr::select(df, 
                              scenario_noinsta0, 
                              weight3,
                              breximp, cooperation3)

ds<- insta_impact %>% 
  filter(complete.cases(.))

mu  <- ds %>%
  group_by(breximp, cooperation3) %>%
  summarize(grp_means = weighted.mean(scenario_noinsta0, weight3))

# Density plots with semi-transparent fill
insta_impact<-ggplot(na.omit(ds), aes(x=scenario_noinsta0, color=breximp, fill=breximp)) + 
  geom_density(aes(weight=weight3),alpha=.2)+
  geom_vline(aes(xintercept = 0),linetype="dotted")+
  geom_vline(data = mu, aes(xintercept = grp_means, 
                            color = breximp), linetype="dashed",size=.7)+ 
  facet_wrap(~cooperation3) + #,labeller = labeller(Scenario=scenario_labs))+
  labs(title="Three years after rejection of framework agreement",
       x="Expected change in Swiss-EU cooperation", y = "Density") + theme_bw()+
  theme(plot.margin = unit(c(1, 1, 2, 1), "lines"),
        text=element_text(family = "LM Roman 10"),
        axis.text=element_text(size=9, color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        legend.position ="bottom",
         legend.text = element_text(size = 11),
        plot.title = element_text(hjust = 0.5, size = 12, face = "bold"),
        plot.caption = element_text(hjust = 1, vjust = -3),
        strip.background = element_rect(colour="black",
                                        fill="white"),
        strip.text = element_text(
          size = 11, #face = "bold"
        ))+
  scale_x_continuous(breaks=c(-8,0,8),labels=x_lab )+ 
  scale_color_discrete(name = "After Brexit, the UK will be", 
                       labels=c("worse", "better/same"))+ guides(fill="none")


insta_impact

# REPLICATE WITH BREXIT STRATEGY

limit_strat <- dplyr::select(df, 
                              scenario_limitation0, 
                              weight3,
                              strategy, cooperation3)

ds<- limit_strat %>% 
  filter(complete.cases(.))

mu  <- ds %>%
  group_by(strategy, cooperation3) %>%
  summarize(grp_means = weighted.mean(scenario_limitation0, weight3))

ds$cooperation3 = as.factor(ds$cooperation3)


x_lab <- c('     Less',"0", "More     ")



# Density plots with semi-transparent fill
lim_strat<-ggplot(na.omit(ds), aes(x=scenario_limitation0, color=strategy, fill=strategy)) + 
  geom_density(aes(weight=weight3),alpha=.2)+
  geom_vline(aes(xintercept = 0),linetype="dotted")+
  geom_vline(data = mu, aes(xintercept = grp_means, 
                            color = strategy), linetype="dashed",size=.7)+ 
  facet_wrap(~cooperation3) + #,labeller = labeller(Scenario=scenario_labs))+
  labs(title="Three years after approval of limitation initiative",
       x="Expected change in Swiss-EU cooperation", y = "Density") + theme_bw()+
  theme(plot.margin = unit(c(1, 1, 2, 1), "lines"),
        text=element_text(family = "LM Roman 10"),
        axis.text=element_text(size=9, color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        legend.position ="bottom",
        legend.text = element_text(size = 11),
        plot.title = element_text(hjust = 0.5, size = 12, face = "bold"),
        plot.caption = element_text(hjust = 1, vjust = -3),
        strip.background = element_rect(colour="black",
                                        fill="white"),
        strip.text = element_text(
          size = 11, #face = "bold"
        ))+
  scale_x_continuous(breaks=c(-8,0,8),labels=x_lab )+ 
  scale_color_discrete(name = "The Brexit strategy is", 
                       labels=c("a cautionary example", "a role model/neither"))+ guides(fill="none")


lim_strat



insta_stra <- dplyr::select(df, 
                              scenario_noinsta0, 
                              weight3,
                              strategy, cooperation3)

ds<- insta_stra %>% 
  filter(complete.cases(.))


mu  <- ds %>%
  group_by(strategy, cooperation3) %>%
  summarize(grp_means = weighted.mean(scenario_noinsta0, weight3))

# Density plots with semi-transparent fill
insta_strat<-ggplot(na.omit(ds), aes(x=scenario_noinsta0, color=strategy, fill=strategy)) + 
  geom_density(aes(weight=weight3),alpha=.2)+
  geom_vline(aes(xintercept = 0),linetype="dotted")+
  geom_vline(data = mu, aes(xintercept = grp_means, 
                            color = strategy), linetype="dashed",size=.7)+ 
  facet_wrap(~cooperation3) + #,labeller = labeller(Scenario=scenario_labs))+
  labs(title="Three years after rejection of framework agreement",
       x="Expected change in Swiss-EU cooperation", y = "Density") + theme_bw()+
  theme(plot.margin = unit(c(1, 1, 2, 1), "lines"),
        text=element_text(family = "LM Roman 10"),
        axis.text=element_text(size=9, color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        legend.position ="bottom",
        legend.text = element_text(size = 11),
        plot.title = element_text(hjust = 0.5, size = 12, face = "bold"),
        plot.caption = element_text(hjust = 1, vjust = -3),
        strip.background = element_rect(colour="black",
                                        fill="white"),
        strip.text = element_text(
          size = 11, #face = "bold"
        ))+
  scale_x_continuous(breaks=c(-8,0,8),labels=x_lab )+ 
  scale_color_discrete(name = "The Brexit strategy is", 
                       labels=c("a cautionary example", "a role model/neither"))+ guides(fill="none")


insta_strat


library(ggpubr)
?ggarrange
impact <- ggarrange(lim_impact,insta_impact, nrow=2, common.legend = TRUE, legend = "bottom") 
impact
ggsave("/Users/malet/Dropbox/Work in progress/Swiss expectations/Graphs/scenario_impact.jpg",  width = 15, height =  13, units = "cm")


strategy <- ggarrange(lim_strat,insta_strat, nrow=2, common.legend = TRUE, legend = "bottom")
strategy
ggsave("/Users/malet/Dropbox/Work in progress/Swiss expectations/Graphs/scenario_strategy.jpg",  width = 15, height =  13, units = "cm")

